package com.tanhua.server.controller;

import com.tanhua.db.User;
<<<<<<< HEAD
=======
import com.tanhua.db.UserInfo;
>>>>>>> acc7004 (第4天到第8天)
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.SettingsVo;
import com.tanhua.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @author： xtr
 * @date： 2021-03-07 19:00
 */
@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    @GetMapping("/settings")
    public ResponseEntity getSettings(){
        SettingsVo settingsVo = settingsService.getSettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 通知设置
     * @param vo
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity updateSettings(@RequestBody SettingsVo vo){
        settingsService.updateSettings(vo);
        return ResponseEntity.ok(null);
    }

    /**
     * 陌生人问题
     * @param map
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity updateQuestion(@RequestBody Map<String,String> map){
        String question = map.get("content");
        settingsService.updateQuestion(question);
        return ResponseEntity.ok(null);
    }

    /**
     * 黑名单
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity findBlackList(@RequestParam(value = "page",defaultValue = "1") long page,
                                        @RequestParam(value = "pagesize",defaultValue = "10") long pagesize){
        //限制一页展示条数pagesize
        pagesize = pagesize>50?50:pagesize;
        PageResult<UserInfoVo> pageResult = settingsService.findBlackList(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 更换手机号发送验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        User user = UserHolder.getUser();
        userService.sendValidateCode(user.getMobile());
        return ResponseEntity.ok(null);
    }

    /**
     * 更换手机号验证验证码
     * @param param
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String,String> param){
        String verificationCode = param.get("verificationCode");
        boolean verification = settingsService.checkVerificationCode(verificationCode);
        Map<String,Boolean> resultMap = new HashMap<>();
        resultMap.put("verification",verification);
        return ResponseEntity.ok(resultMap);
    }

    /**
     * 更换手机号绑定手机号
     * @param param
     * @param token
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map<String,String> param,
                                    @RequestHeader("Authorization") String token){
        String phone = param.get("phone");
        settingsService.savePhone(phone,token);
        return ResponseEntity.ok(null);
    }

    /**
     * 删除黑名单
     * @param uid--要删除的黑名单用户id  black_user_id
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable long uid){
        settingsService.deleteBlackList(uid);
        return ResponseEntity.ok(null);
    }
}
